
//
//  LYSettingUsernameController.m
//  SelectCity
//
//  Created by liuyan on 2019/3/26.
//  Copyright © 2019年 liuyan. All rights reserved.
//

#import "LYSettingUsernameController.h"

@interface LYSettingUsernameController ()<UITextFieldDelegate>

@property (nonatomic, strong) UILabel *remainingLabel;
@property (nonatomic, strong) UITextField *nameTF;

@end

@implementation LYSettingUsernameController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"修改用户名";
    
    [self createSubview];
}

- (void)createSubview{
    self.nameTF = [[UITextField alloc] initWithFrame:CGRectMake(15, 6, WIDTH - 30, 50)];
    self.nameTF.placeholder = @"请输入用户名";
    self.nameTF.textColor = THEMECOLOR_OF_TEXT_333333;
    self.nameTF.font = [UIFont systemFontOfSize:16];
    self.nameTF.delegate = self;
    self.nameTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.nameTF.text = self.name;
    [self.view addSubview:self.nameTF];
    
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, self.nameTF.bottom, WIDTH, lineH)];
    lineView.backgroundColor = THEMECOLOR_OF_TEXT_cccccc;
    [self.view addSubview:lineView];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, lineView.bottom +5, WIDTH-75, 15)];
    titleLabel.font = [UIFont systemFontOfSize:11];
    titleLabel.textColor = [UIColor orangeColor];
    titleLabel.adjustsFontSizeToFitWidth = YES;
    titleLabel.text = @"用户名修改后，登录名将同步修改";
    [self.view addSubview:titleLabel];
    
    self.remainingLabel = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH - 60, lineView.bottom +5, 50, 15)];
    self.remainingLabel.font = [UIFont systemFontOfSize:12];
    self.remainingLabel.textColor = [UIColor redColor];
    self.remainingLabel.adjustsFontSizeToFitWidth = YES;
    self.remainingLabel.textAlignment = NSTextAlignmentRight;
    [self.view addSubview:self.remainingLabel];
    if (self.name && self.name.length) {
        [self showLabelMessagewithCount:self.name.length];
    } else {
        [self showLabelMessagewithCount:0];
    }
    
    UIButton *surebutton = [UIButton buttonWithType:UIButtonTypeCustom];
    surebutton.frame = CGRectMake( 40, lineView.bottom + 150, WIDTH-80, 40);
    surebutton.layer.cornerRadius = surebutton.height/2;
    surebutton.clipsToBounds = YES;
    surebutton.backgroundColor = [UIColor orangeColor];
    [surebutton setTitle:@"确定" forState:UIControlStateNormal];
    [surebutton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    surebutton.titleLabel.font = [UIFont systemFontOfSize:14];
    [surebutton addTarget:self action:@selector(surebuttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:surebutton];
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSMutableString *multStr = [NSMutableString stringWithString:textField.text];
    [multStr replaceCharactersInRange:range withString:string];
    if (multStr.length > 24) {
        return NO;
    }else{
        [self showLabelMessagewithCount:multStr.length];
        return YES;
    }
}

- (void)showLabelMessagewithCount:(NSUInteger)count{
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%lu",(unsigned long)count]];
    NSAttributedString *strq = [[NSAttributedString alloc] initWithString:@"/24个字符" attributes:@{NSForegroundColorAttributeName :THEMECOLOR_OF_TEXT_9a9a9a,NSFontAttributeName : Font(Font_12)}];
    
    [str appendAttributedString:strq];
    _remainingLabel.attributedText = str;
}

#pragma mark -- Action
- (void)surebuttonClick:(UIButton *)button{
    SVPHUDShow;
    [self.nameTF resignFirstResponder];
    NSString *name = self.nameTF.text;
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:0];
    dic = [UIToolsControl getPublicParameter];
    dic[@"username"] = name;
    __weak typeof(self) weakSelf = self;
    [BaseNetManager requestWithMethod:POST path:Setting_bindUserName_URL params:dic successBlock:^(NSDictionary *responseObject) {
        if ([responseObject[@"code"] integerValue] == 2000) {
            //绑定成功
            SVPHUDSuccess(responseObject[@"msg"]);
            UD_SetOForK(name, isUserName);
            weakSelf.block();
            [weakSelf.navigationController popViewControllerAnimated:YES];
        }else{
            SVPHUDError(responseObject[@"msg"]);
        }
    } failurBlock:^(NSError *error) {
        SVPHUDError(NetErrorAlertText);
    }];
}

@end
